/**
 * jQuery CMS Plugin
 * 
 * @version: 0.2
 */
(function($) {
	
	/**
	 * 
	 */
	$.windowLocationSearch = function() {
		var params = window.location.search.substring(1).split("&");
		var paramObj = {};
		for ( var i = 0; i<params.length ; i++) {
			var param = params[i];
			paramObj[param.substring(0, param.indexOf("="))] = param.substring(param
					.indexOf("=") + 1);
		}
		return paramObj;
	};

	/**
	 * 
	 * 
	 */
	$.openDialog = function(div, options, callback) {
		$(div).dialog("destroy");
		var _options = {
			"autoOpen" : true,
			"modal" : true,
			"height" : 600,
			"width" : 760,
			"buttons" : {
				'确认' : function() {
					if (!_beforeConfirm(callback)) {
						return;
					}
					$(this).dialog("close");
				}
			}
		};
		$.extend(_options, options);
		var dialog = $(div);
		if (options.loadPage) {
			dialog.load(options.loadPage);
		}
		dialog.dialog(_options).dialog("open");
	};

	/**
	 * 
	 * 
	 */
	$.openReadonlyDialog = function(div, options) {
		$(div).dialog("destroy");
		var _options = {
			"autoOpen" : true,
			"modal" : true,
			"height" : 600,
			"width" : 760,
			"buttons" : {
				'确认' : function() {
					$(this).dialog("close");
				}
			}
		};
		$.extend(_options, options);
		var dialog = $(div);
		if (options.loadPage) {
			dialog.load(options.loadPage);
		}
		dialog.dialog(_options).dialog("open");
	};
	
	/**
	 * 打开选择地图座标对话框
	 */
	$.openPointSelect = function(div, options, callback) {
		$(div).dialog("destroy");
		var _options = {
			"autoOpen" : true,
			"modal" : true,
			"height" : 520,
			"width" : 630,
			"minHeight" : 520,
			"minWidth" : 630,
			"buttons" : {
				'确认' : function() {
					if (!_beforePointConfirm(callback)) {
						return;
					}
					$(this).dialog("close");
				}
			}
		};
		$.extend(_options, options);
		var dialog = $(div);
		if (options.loadPage) {
			dialog.load(options.loadPage);
		}
		dialog.dialog(_options).dialog("open");
	};


	/**
	 * JQuery UI Dialog -> window.alert
	 */
	$.alert = function(message) {
		var dialog = $("<div title='提示'><p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 50px 0;'></span></p></div>");
		dialog.find("p").append(message);
		dialog.dialog( {
			modal : true,
			buttons : {
				'确定' : function() {
					$(this).dialog("close");
					$(this).dialog("destroy");
				}
			}
		}).dialog('open');
	};

	$.info = function(message) {
		var dialog = $("<div title='提示'><p><span class='ui-icon ui-icon-info' style='float:left; margin:0 7px 50px 0;'></span></p></div>");
		dialog.find("p").append(message);
		dialog.dialog( {
			modal : true,
			buttons : {
				'确定' : function() {
					$(this).dialog("close");
					$(this).dialog("destroy");
				}
			}
		}).dialog('open');
	};

	$.notice = function(message) {
		var dialog = $("<div title='提示'><p><span class='ui-icon ui-icon-notice' style='float:left; margin:0 7px 50px 0;'></span></p></div>");
		dialog.find("p").append(message);
		dialog.dialog( {
			modal : true,
			buttons : {
				'确定' : function() {
					$(this).dialog("close");
					$(this).dialog("destroy");
				}
			}
		}).dialog('open');
	};

	$.readonlyTree = function(div, url, checkboxSettings) {
		if (checkboxSettings) {
			var _checkboxSettings_ = {two_state:true};
			_checkboxSettings_ = $.extend(_checkboxSettings_,checkboxSettings);
			
			return $(div).jstree({
				themes: {"theme" : "default"},
				json_data: { 
					ajax : {
						"url" : url,
						"method" : "GET",
						"async" : true,
						"cache" : false,
						"data" : function(node) {
							return {id:node.data ? node.data("id") : ""};
						},
						"success": function(data) {
							if (!data.succeed) {
								return [];
							}
							return data.data;
						}
					}
				},
				checkbox: _checkboxSettings_,
				languages: ["zh"],
				plugins: ["themes","json_data","ui","languages","checkbox"]
			});
		}
		
		return $(div).jstree({
			themes: {"theme" : "default"},
			json_data: { 
				ajax : {
					"url" : url,
					"method" : "GET",
					"async" : true,
					"cache" : false,
					"data" : function(node) {
						return {id:node.data ? node.data("id") : ""};
					},
					"success": function(data) {
						if (!data.succeed) {
							return [];
						}
						return data.data;
					}
				}
			},
			languages: ["zh"],
			plugins: ["themes","json_data","ui","languages"]
		});
	};
 

})(jQuery);
